﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;
using System.Collections;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore.I_DAL;
using System.Data;

namespace STHPMS1.BLL.B_Bus
{
    public class Dp_Prevemeas_IniBus : BllBase, IDp_Prevemeas_IniBill
    {
        public void Save<T>(T entity)
            where T : class ,IDp_Prevemeas_Ini
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IDp_Prevemeas_Ini entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IDp_Prevemeas_Ini
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info,k=>k.Mainsegid,false);
        }

        /// <summary>
        /// 提取主要病害
        /// </summary>
        /// <param name="rdwayid"></param>
        public void Insert_ASP(string anniname,string analy_year)
        {
            Hashtable columht = GetColumNameHasTable();
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            dao.Delete<Dp_Prevemeas_Ini>(p => p.Anni_Name == anniname && p.Analy_Year == analy_year);
            

            ///获取路段
            Expression<Func<Dn_Divi_Planning, bool>> exp = null;
            if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anniname.ToString() && pre.Divi_Time == analy_year.ToString() && pre.Segid_Main == 12001;
            List<Dn_Divi_Planning> list = this.Scene.Bll.Dn_Divi_Planning.Get<Dn_Divi_Planning>(exp, null).ToList<Dn_Divi_Planning>();
            foreach (Dn_Divi_Planning info in list)
            {
                ////获取三种最主要的病害
                DataTable dt = GetHasTable_ASP(info.Mainsegid);
                Dp_Prevemeas_Ini dp_info = new Dp_Prevemeas_Ini();
                dp_info.Id = serv.NewGuid();
                dp_info.Anni_Name = info.Anni_Name;//资金模型参数
                dp_info.RoadnetId = info.RoadnetId; //路网ID
                dp_info.Rdwayid =info.Rdwayid;  //路线编号
                dp_info.Mainsegid = info.Mainsegid; //路段ID

                //dp_info.Rd_Class = info.Rdclass_Dictid;
                dp_info.Rd_Class = this.Scene.Bll.Dp_Dailymain_Cost.GetB_Invntory(info.Mainsegid).Rdclass_Dictid; //技术等级
                if(dt.Rows.Count==1)
                {
                    dp_info.Maindisea_1 = columht[dt.Rows[0]["htKey"]].ToString();
                    dp_info.Maindisea_2 = "";
                    dp_info.Maindisea_3 = "";
                }
                if (dt.Rows.Count == 2)
                {
                    dp_info.Maindisea_1 = columht[dt.Rows[0]["htKey"]].ToString();
                    dp_info.Maindisea_2 = columht[dt.Rows[1]["htKey"]].ToString();
                    dp_info.Maindisea_3 = "";
                }
                if (dt.Rows.Count >= 3)
                {
                    dp_info.Maindisea_1 = columht[dt.Rows[0]["htKey"]].ToString();
                    dp_info.Maindisea_2 = columht[dt.Rows[1]["htKey"]].ToString();
                    dp_info.Maindisea_3 = columht[dt.Rows[2]["htKey"]].ToString();
                }
              

                dp_info.Analy_Year = info.Divi_Time; //分析年度：决策路段表中规划年度
                Save<Dp_Prevemeas_Ini>(dp_info);
            }
        }

        /// <summary>
        /// 病害表中提取所有病害
        /// </summary>
        /// <param name="segid"></param>
        /// <returns></returns>
        public DataTable GetHasTable_ASP(string segid)
        {
            
            DataTable dt = GetMainSegid_ASP(segid);

            
            //SortedList ht = new SortedList(new MyComparer());

            Hashtable ht = new Hashtable();
          
            Hashtable newht = new Hashtable();
            ht.Add("ALGCK_LM", dt.Rows[0]["ALGCK_LM"]);
            ht.Add("ALGCK_H", dt.Rows[0]["ALGCK_H"]);
            ht.Add("BLFT_L", dt.Rows[0]["BLFT_L"]);
            ht.Add("BLFT_H", dt.Rows[0]["BLFT_H"]);
            ht.Add("CRACK_L", dt.Rows[0]["CRACK_L"]);

            ht.Add("CRACK_H", dt.Rows[0]["CRACK_H"]);
            ht.Add("PIT_L", dt.Rows[0]["PIT_L"]);
            ht.Add("PIT_H", dt.Rows[0]["PIT_H"]);
            ht.Add("LOOSE_L", dt.Rows[0]["LOOSE_L"]);
            ht.Add("LOOSE_H", dt.Rows[0]["LOOSE_H"]);
            ht.Add("SINK_L", dt.Rows[0]["SINK_L"]);
            ht.Add("SINK_H", dt.Rows[0]["SINK_H"]);
            ht.Add("PLWV", dt.Rows[0]["PLWV"]);
            ht.Add("RUT_L", dt.Rows[0]["RUT_L"]);
            ht.Add("RUT_H", dt.Rows[0]["RUT_H"]);
            ht.Add("bled", dt.Rows[0]["bled"]);
            ht.Add("repair", dt.Rows[0]["repair"]);

            DataTable dts = new DataTable();
            dts.Columns.Add("htKey", typeof(string));
            dts.Columns.Add("htValue", typeof(double));


            IDictionaryEnumerator ide = ht.GetEnumerator();
            while (ide.MoveNext())
            {
                if(Convert.ToDouble(ide.Value)!=0)
                { 
                    DataRow dr = dts.NewRow();
                    dr["htKey"] = ide.Key.ToString();
                    dr["htValue"] = ide.Value.ToString();
                    dts.Rows.Add(dr);
                }
            }

            DataView dv = dts.DefaultView;
            dv.Sort = "htValue Desc";//此处是按值降序排列
            DataTable dt1 = dv.ToTable();
            return dt1;

        }

        /// <summary>
        /// 病害表中提取所有病害
        /// </summary>
        /// <param name="segid"></param>
        /// <returns></returns>
        public DataTable GetMainSegid_ASP(string segid)
        {
            return this.Scene.Dal.Sthpms1.Dp_Prevemeas_Ini.GetMainSegid_ASP(segid);
        }



        ///// <summary>
        ///// 更新初选措施   已经无效,找到更好的方法
        ///// </summary>
        ///// <param name="rdwaryid"></param>
        ///// <param name="mainsegid"></param>
        ///// <param name="meas_ini"></param>
        //public void SetMEAS_INI(string rdwaryid, string mainsegid, string meas_ini)
        //{
        //    this.Scene.Dal.Sthpms1.Dp_Prevemeas_Ini.SetMEAS_INI(rdwaryid, mainsegid, meas_ini);
        //}


        /// <summary>
        /// 根据路线编号，获取初选措施
        /// </summary>
        /// <param name="rdwayid"></param>
        public void EditMEAS_INI(string anniname,string analy_year)
        {
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();

            Expression<Func<Dp_Prevemeas_Ini, bool>> exp = null;
            if (analy_year != null && analy_year.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year == analy_year.ToString();
            List<Dp_Prevemeas_Ini> list = this.Scene.Bll.Dp_Prevemeas_Ini.Get<Dp_Prevemeas_Ini>(exp, null).ToList<Dp_Prevemeas_Ini>();

            for (int i = 0; i < list.Count();i++ )
            {
                string sqlcolum = string.Empty;
                string strApply_Measure=string.Empty;
                Dp_Prevemeas_Ini info = list[i];
                Hashtable ht= GetColumHasTable();
                string strsearch = string.Empty;
                if (!string.IsNullOrEmpty(info.Maindisea_1))
                {
                    strsearch += "and " + Convert.ToString(ht[info.Maindisea_1]) + "=1";
                }
                if (!string.IsNullOrEmpty(info.Maindisea_2))
                {
                    strsearch += "and " + Convert.ToString(ht[info.Maindisea_2]) + "=1";
                }
                if (!string.IsNullOrEmpty(info.Maindisea_3))
                {
                    strsearch += "and " + Convert.ToString(ht[info.Maindisea_3]) + "=1";
                }
                //string str1 = Convert.ToString(ht[info.Maindisea_1]); //病害1
                //string str2 = Convert.ToString(ht[info.Maindisea_2]); //病害2
                //string str3 = Convert.ToString(ht[info.Maindisea_3]); //病害3

                //List<Dp_Preven_Meaint> list_dp = this.Scene.Bll.Dp_Preven_Meaint.Get<Dp_Preven_Meaint>(str1, str2, str3, 160);
                //List<Dp_Preven_Meaint> list_dp = this.Scene.Bll.Dp_Preven_Meaint.Get<Dp_Preven_Meaint>(str1, str2, str3, Convert.ToInt32(info.Rd_Class)); //获取初选措施
                List<Dp_Preven_Meaint> list_dp = this.Scene.Bll.Dp_Preven_Meaint.Get<Dp_Preven_Meaint>(strsearch, Convert.ToInt32(info.Rd_Class)); //获取初选措施

                ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
                dao.Delete<Dp_Preven_Detail>(p =>p.Anni_Name==anniname && p.Analy_Year == analy_year && p.Mainsegid == info.Mainsegid);

                for (int j = 0; j < list_dp.Count(); j++)
                {
                    strApply_Measure += list_dp[j].Apply_Measure + "|";

                    //插入预防养护措施流程表：PREVEN_DETAIL表
                    Dp_Preven_Detail Detailinfo = new Dp_Preven_Detail();
                    Detailinfo.Id = serv.NewGuid();
                    Detailinfo.Anni_Name = info.Anni_Name; //资金模型名称
                    Detailinfo.RoadnetId = info.RoadnetId;//路网ID
                    Detailinfo.Rdwayid = info.Rdwayid; //路线编号
                    Detailinfo.Mainsegid = info.Mainsegid; //路段
                    Detailinfo.Rd_Class = info.Rd_Class;
                    Detailinfo.Meas_Ini = list_dp[j].Apply_Measure; //措施

                    //Dp_Preven_Measec measecinfo= dao.SelectByIds<Dp_Preven_Measec>(list_dp[j].Apply_Measure)[0];
                    Dp_Preven_Measec measecinfo = dao.SingleById<Dp_Preven_Measec>(list_dp[j].Apply_Measure);
                    Detailinfo.Servi_Life = measecinfo.Servi_Life;  //使用寿命/年
                    Detailinfo.Unit_Cost = measecinfo.Unit_Cost; //单位成本(元/m2)

                    Detailinfo.Analy_Year = info.Analy_Year; //分析年度 


                    this.Scene.Bll.Dp_Preven_Detail.Save<Dp_Preven_Detail>(Detailinfo);
                }

                //更新初选措施到DP_PREVEMEAS_INI表中
                //SetMEAS_INI(rdwayid, info.Mainsegid, strApply_Measure);
                //dao.Update<Dp_Prevemeas_Ini>(new { Meas_Ini = strApply_Measure }, p => p.Analy_Year == analy_year && p.Mainsegid == info.Mainsegid);
                dao.Update<Dp_Prevemeas_Ini>(new { Meas_Ini = strApply_Measure }, p => p.Id == info.Id);
                
            }
        }


        public Hashtable GetColumHasTable()
        {
            Hashtable ht = new Hashtable();
            ht.Add("轻、中度龟裂", "ALGCK_LM");
            ht.Add("重度龟裂", "ALGCK_H");
            ht.Add("轻度块裂", "BLFT_L");
            ht.Add("重度块裂", "BLFT_H");
            ht.Add("轻度裂缝", "CRACK_L");
            ht.Add("重度裂缝", "CRACK_H");
            ht.Add("轻度坑槽", "PIT_L");
            ht.Add("重度坑槽", "PIT_H");
            ht.Add("轻度松散", "LOOSE_L");
            ht.Add("重度松散", "LOOSE_H");
            ht.Add("轻度沉陷", "SINK_L");
            ht.Add("重度沉陷", "SINK_H");
            ht.Add("波浪拥包", "PLWV    ");
            ht.Add("轻度车辙", "RUT_L");
            ht.Add("重度车辙", "RUT_H");
            ht.Add("泛油", "bled");
            ht.Add("修补", "repair");
            return ht;
        }

        public Hashtable GetColumNameHasTable()
        {
            Hashtable ht = new Hashtable();
            ht.Add("ALGCK_LM", "轻、中度龟裂");
            ht.Add("ALGCK_H", "重度龟裂");
            ht.Add("BLFT_L", "轻度块裂");
            ht.Add("BLFT_H", "重度块裂");
            ht.Add("CRACK_L", "轻度裂缝");
            ht.Add("CRACK_H", "重度裂缝");
            ht.Add("PIT_L", "轻度坑槽");
            ht.Add("PIT_H", "重度坑槽");
            ht.Add("LOOSE_L", "轻度松散");
            ht.Add("LOOSE_H", "重度松散");
            ht.Add("SINK_L", "轻度沉陷");
            ht.Add("SINK_H", "重度沉陷");
            ht.Add("PLWV", "波浪拥包");
            ht.Add("RUT_L", "轻度车辙");
            ht.Add("RUT_H", "重度车辙");
            ht.Add("bled", "泛油");
            ht.Add("repair", "修补");
            return ht;
        }

    }
}
